package hos.http;

import java.io.IOException;
import java.util.Map;

/**
 * <p>Title </p>
 * <p>Description:  </p>
 * <p>Company: www.mapuni.com </p>
 *
 * @author : 蔡俊峰
 * @version : 1.0
 * @date : 2023/11/22 18:56
 */
public class HttpException extends IOException {

    HttpErrorType type;
    String url;
    Map<String, Object> param;
    Object tag;

    String code;

    public HttpException(HttpErrorType type, String url,
                         Map<String, Object> param, Throwable cause, Object tag) {
        super(type.getMessage(), cause);
        this.type = type;
        this.url = url;
        this.param = param;
        this.tag = tag;
    }

    public HttpException(HttpErrorType type, String url,
                         Map<String, Object> param, Throwable cause) {
        this(type, url, param, cause, null);
    }

    public HttpException(HttpErrorType type, String url,
                         Map<String, Object> param) {
        this(type, url, param, null, null);
    }

    public HttpException(HttpErrorType type, String url,
                         Throwable cause) {
        this(type, url, null, cause, null);
    }

    public HttpException(HttpErrorType type, String url) {
        this(type, url, null, null, null);
    }


    public String path() {
        if (param == null || param.isEmpty()) {
            return url;
        }
        return url + "?" + HttpUtils.urlParam(param);
    }

    public String msg() {
        String message = getMessage();
        if (message == null || message.isEmpty()) {
            return HttpConstant.SERVER_ERROR;
        }
        return message;
    }

    public HttpErrorType getType() {
        return type;
    }

    @Override
    public String getLocalizedMessage() {
        return msg() + " " + path();
    }

    @Override
    public String toString() {
        return type.getMessage() + " " + path() + " " + getMessage() + " " + getCause();
    }


    public static HttpException throwTimeout(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.timeout, url, param, cause);
    }

    public static HttpException throwTimeout(
            String url
    ) {
        return new HttpException(HttpErrorType.timeout, url);
    }

    public static HttpException throwTimeout(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.timeout, url, param);
    }

    public static HttpException throwTimeout(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.timeout, url, cause);
    }


    public static HttpException throwCertificate(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.certificate, url, param, cause);
    }

    public static HttpException throwCertificate(
            String url
    ) {
        return new HttpException(HttpErrorType.certificate, url);
    }

    public static HttpException throwCertificate(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.certificate, url, cause);
    }

    public static HttpException throwCertificate(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.certificate, url, param);
    }


    public static HttpException throwResponse(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.response, url, param, cause);
    }

    public static HttpException throwResponse(
            String url
    ) {
        return new HttpException(HttpErrorType.response, url);
    }

    public static HttpException throwResponse(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.response, url, cause);
    }

    public static HttpException throwResponse(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.response, url, param);
    }


    public static HttpException throwCancel(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.cancel, url, param, cause);
    }

    public static HttpException throwCancel(
            String url
    ) {
        return new HttpException(HttpErrorType.cancel, url);
    }

    public static HttpException throwCancel(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.cancel, url, cause);
    }

    public static HttpException throwCancel(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.cancel, url, param);
    }


    public static HttpException throwConnection(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.connection, url, param, cause);
    }

    public static HttpException throwConnection(
            String url
    ) {
        return new HttpException(HttpErrorType.connection, url);
    }

    public static HttpException throwConnection(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.connection, url, cause);
    }

    public static HttpException throwConnection(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.connection, url, param);
    }


    public static HttpException throwUnknown(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.unknown, url, param, cause);
    }

    public static HttpException throwUnknown(
            String url
    ) {
        return new HttpException(HttpErrorType.unknown, url);
    }

    public static HttpException throwUnknown(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.unknown, url, cause);
    }

    public static HttpException throwUnknown(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.unknown, url, param);
    }


    public static HttpException throwConverter(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.converter, url, param, cause);
    }

    public static HttpException throwConverter(
            String url
    ) {
        return new HttpException(HttpErrorType.converter, url);
    }

    public static HttpException throwConverter(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.converter, url, cause);
    }

    public static HttpException throwConverter(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.converter, url, param);
    }


    public static HttpException throwDownload(
            String url,
            Map<String, Object> param,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.download, url, param, cause, tag);
    }

    public static HttpException throwDownload(
            String url
    ) {
        return new HttpException(HttpErrorType.download, url);
    }

    public static HttpException throwDownload(
            String url,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.download, url, null, cause, tag);
    }

    public static HttpException throwDownload(
            String url,
            Map<String, Object> param,
            Object tag
    ) {
        return new HttpException(HttpErrorType.download, url, param, null, tag);
    }

    public static HttpException throwDownload(
            String url,
            Object tag
    ) {
        return new HttpException(HttpErrorType.download, url, null, null, tag);
    }

    public static HttpException throwDownload(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.download, url, cause);
    }

    public static HttpException throwDownload(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.download, url, param);
    }


    public static HttpException throwUpload(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.upload, url, param, cause);
    }

    public static HttpException throwUpload(
            String url
    ) {
        return new HttpException(HttpErrorType.upload, url);
    }

    public static HttpException throwUpload(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.upload, url, cause);
    }

    public static HttpException throwUpload(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.upload, url, param);
    }


    public static HttpException throwFailure(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.failure, url, param, cause);
    }

    public static HttpException throwFailure(
            String url
    ) {
        return new HttpException(HttpErrorType.failure, url);
    }

    public static HttpException throwFailure(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.failure, url, cause);
    }

    public static HttpException throwFailure(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.failure, url, param);
    }


    public static HttpException throwServer(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.server, url, param, cause);
    }

    public static HttpException throwServer(
            String url
    ) {
        return new HttpException(HttpErrorType.server, url);
    }

    public static HttpException throwServer(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.server, url, cause);
    }

    public static HttpException throwServer(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.server, url, param);
    }


    public static HttpException throwNetwork(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.network, url, param, cause);
    }

    public static HttpException throwNetwork(
            String url
    ) {
        return new HttpException(HttpErrorType.network, url);
    }

    public static HttpException throwNetwork(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.network, url, cause);
    }

    public static HttpException throwNetwork(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.network, url, param);
    }


    public static HttpException throwParam(
            String url,
            Map<String, Object> param,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.param, url, param, cause, tag);
    }

    public static HttpException throwParam(
            String url
    ) {
        return new HttpException(HttpErrorType.param, url);
    }

    public static HttpException throwParam(
            String url,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.param, url, null, cause, tag);
    }

    public static HttpException throwParam(
            String url,
            Map<String, Object> param,
            Object tag
    ) {
        return new HttpException(HttpErrorType.param, url, param, null, tag);
    }

    public static HttpException throwParam(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.param, url, cause);
    }

    public static HttpException throwParam(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.param, url, param);
    }


    public static HttpException throwUrlParse(
            String url,
            Map<String, Object> param,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.urlParse, url, param, cause);
    }

    public static HttpException throwUrlParse(
            String url
    ) {
        return new HttpException(HttpErrorType.urlParse, url);
    }

    public static HttpException throwUrlParse(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.urlParse, url, cause);
    }

    public static HttpException throwUrlParse(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.urlParse, url, param);
    }


    public static HttpException throwResult(
            String url,
            Map<String, Object> param,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.result, url, param, cause, tag);
    }

    public static HttpException throwResult(
            String url
    ) {
        return new HttpException(HttpErrorType.result, url);
    }

    public static HttpException throwResult(
            String url,
            Object tag
    ) {
        return new HttpException(HttpErrorType.result, url, null, null, tag);
    }

    public static HttpException throwResult(
            String url,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.result, url, null, cause, tag);
    }

    public static HttpException throwResult(
            String url,
            Map<String, Object> param,
            Object tag
    ) {
        return new HttpException(HttpErrorType.result, url, param, null, tag);
    }

    public static HttpException throwResult(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.result, url, cause);
    }

    public static HttpException throwResult(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.result, url, param);
    }


    public static HttpException throwResultNull(
            String url,
            Map<String, Object> param,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.resultNull, url, param, cause, tag);
    }

    public static HttpException throwResultNull(
            String url,
            Object tag
    ) {
        return new HttpException(HttpErrorType.resultNull, url, null, null, tag);
    }

    public static HttpException throwResultNull(
            String url
    ) {
        return new HttpException(HttpErrorType.resultNull, url);
    }

    public static HttpException throwResultNull(
            String url,
            Throwable cause,
            Object tag
    ) {
        return new HttpException(HttpErrorType.resultNull, url, null, cause, tag);
    }

    public static HttpException throwResultNull(
            String url,
            Map<String, Object> param,
            Object tag
    ) {
        return new HttpException(HttpErrorType.resultNull, url, param, null, tag);
    }

    public static HttpException throwResultNull(
            String url,
            Throwable cause
    ) {
        return new HttpException(HttpErrorType.resultNull, url, cause);
    }

    public static HttpException throwResultNull(
            String url,
            Map<String, Object> param
    ) {
        return new HttpException(HttpErrorType.resultNull, url, param);
    }
}
